package com.miaosha.controller;

import com.miaosha.annotation.AuthToken;
import com.miaosha.entity.Role;
import com.miaosha.entity.dto.RoleDto;
import com.miaosha.service.RoleService;
import com.miaosha.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@AuthToken
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @GetMapping("/list")
    public CommonResult<List<Role>> list() {

        List<Role> roles = roleService.list();
        return CommonResult.success(roles);
    }

    @PostMapping("/addRole")
    public CommonResult<String> addRole(@RequestBody RoleDto roleDto){
        roleService.saveWithPermissions(roleDto);
        return CommonResult.success("Success");
    }

    @GetMapping("listWithPermissions")
    public CommonResult<List<RoleDto>> getRoleList(){

        List<RoleDto> roleDtos = roleService.listRoleDto();
        return CommonResult.success(roleDtos);
    }

    @DeleteMapping("/{id}")
    public CommonResult<?> deleteById(@PathVariable Long id){

        roleService.deleteWithPermissions(id);
        return CommonResult.success("success");
    }

    @GetMapping("/{roleId}")
    public CommonResult<RoleDto> getById(@PathVariable Long roleId){

        RoleDto roleDto = roleService.getByIdWithPermissions(roleId);
        return CommonResult.success(roleDto);
    }

    @PutMapping("updateRole")
    public CommonResult<?> updateRole(@RequestBody RoleDto roleDto){

        roleService.updateWithPermissions(roleDto);

        return CommonResult.success("success");
    }
}
